package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.JOptionPane;

import bean.Petbean;
import DAO.DaoFactory;
import service.IPetDao;


/**
 * 商品管理Servlet，处理商品删除、增加、修改、新增等操作
 * @author Administrator
 *
 */
public class OperatorServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public OperatorServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		//获取前台参数，根据参数选择对应的servlet方法进行业务处理
		String op = request.getParameter("op");
		
		if("update".equals(op)){//更新
			updateService(request,response);
		}else if("del".equals(op)){//删除
			deleteService(request,response);
		}else if("insert".equals(op)){//添加
			insertService(request,response);
		}else if("select".equals(op)){//查询
			selectService(request,response);
		}
		else if("find".equals(op)){//查询
			FindService(request,response);
	}}
	
	
	
	private void FindService(HttpServletRequest request, HttpServletResponse response) {
		// TODO 自动生成的方法存根
		int id =Integer.parseInt(request.getParameter("id"));
		Petbean p = new Petbean();
		p.setId(id);
		IPetDao dao = DaoFactory.getProductDao();
		  try{
		  if(dao.queryById(p)!=null){
			  request.getRequestDispatcher("查找宠物信息.jsp").forward(request, response);
		  }else{
			  JOptionPane.showMessageDialog(null, "该宠物ID不存在", "提示",JOptionPane.ERROR_MESSAGE);
		  }
		  }
		  catch(Exception e){
			  e.printStackTrace();
		  }
	}

	/**
	 * 	宠物信息
	 * @param response
	 */
	private void selectService(HttpServletRequest request,
			HttpServletResponse response) {
		
		  IPetDao dao = DaoFactory.getProductDao();
		  Map<String,Object> map = null;

		  try{
		  if(dao.queryPet(map)!=null){
			  request.getRequestDispatcher("宠物信息.jsp").forward(request, response);
		  }else{
			  response.sendRedirect("main1.jsp");
		  }
		  }
		  catch(Exception e){
			  e.printStackTrace();
		  }
	}
	/**
	 * 增加宠物
	 * @param request
	 * @param response
	 */
	private void insertService(HttpServletRequest request,
			HttpServletResponse response) {
		
		//获取前台传递的三个参数，其中id在数据中使用自增字段，无需手动输入
		int id=Integer.parseInt(request.getParameter("id"));
		String name=request.getParameter("name");
		String type=request.getParameter("type");
		String birthday=request.getParameter("birthday");
		String own_name=request.getParameter("own_name");	
        Petbean p=new Petbean(id, name, type, birthday, own_name);
		IPetDao dao = DaoFactory.getProductDao();
		try{
		if(dao.insert(p)){
			request.getRequestDispatcher("宠物信息.jsp").forward(request, response);
		}else{
			response.sendRedirect("增加宠物.jsp");
		}}
		catch(Exception e){
			e.printStackTrace();
		}
		
	}
	/**
	 * 删除宠物
	 * @param request
	 * @param response
	 */
	private void deleteService(HttpServletRequest request,
			HttpServletResponse response) {
		
		//接收前台传递的待删除商品ID，根据id删除对应商品
		//delete from product where id ="id";
		int id =Integer.parseInt(request.getParameter("id"));
		Petbean p = new Petbean();
		p.setId(id);
		IPetDao dao = DaoFactory.getProductDao();
		try{
		if(dao.delete(p)){
			request.getRequestDispatcher("宠物信息.jsp").forward(request, response);
			
		}else{
			response.sendRedirect("宠物信息.jsp");
		}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	/**
	 * 更新宠物
	 * @param request
	 * @param response
	 */
	private void updateService(HttpServletRequest request,
			HttpServletResponse response) {
		//获取前台更新商品的参数，根据商品id进行更新
		//update product set 
		//product.name ="xxx",product.addr="xxx",product.price="xxx" 
		//where id ="待删除商品id";
		
		int id = Integer.parseInt(request.getParameter("id"));
		String name=request.getParameter("name");
		String type=request.getParameter("type");
		String birthday=request.getParameter("birthday");
		String own_name=request.getParameter("own_name");	
		 
		  Petbean p=new Petbean(id, name, type, birthday, own_name);
		  IPetDao dao = DaoFactory.getProductDao();
		  try{
		  if(dao.update(p)){
			  request.getRequestDispatcher("宠物信息.jsp").forward(request, response);
		  }else{
			  response.sendRedirect("更新宠物.jsp");
		  }
		  }
		  catch(Exception e){
			  e.printStackTrace();
		  }
	}

	public void init() throws ServletException {
	}

}
